﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.CarFriend.Model;
using System.Data.Common;
using System.Data;
using WISD.Common;
using WISD.CarFriend.BLL;
using WISD.CarFriend.BLL.Web;

namespace WISD.CarFriend.Web.Module.Information
{
    public partial class InfoDetail : System.Web.UI.Page
    {
        #region 字段
        private int m_CurrentPageIndex;    // 当前页
        private int m_PageCount;    // 总条数
        private string m_Keywords = "";    // 分页传递查询条件
        public readonly int m_PageSize = 12;    // 每页显示条数
        private int m_InfoID;
        public string m_Title = string.Empty;
        public string InfoTitle = string.Empty;
        public string InfoContent = string.Empty;
        public string CreateDate = string.Empty;
        public T_CF_Web_Information m_Info = new T_CF_Web_Information();
        #region 备注
        // 确保页面中的服务器控件完成功能后，绑定数据，否则会导致分页异常。
        // 异常详述：
        // 服务器控件会触发页面Page_Load方法，
        // 服务器控件触发的类的实例与第一次加载的类的实例不是同一实例，
        // 所以m_PageCount会被初始化为0，导致大量数据却只有一个分页的异常。
        // 第二种解决办法：
        // 将m_PageCount变为静态变量，这样即使两个实例不一致，但m_PageCount数据一致。
        // 但这样会导致全局记忆，即用户关闭该页面，但信息仍然保存，下次打开时，就会产生冲突。
        #endregion
        #endregion


        #region 属性
        /// <summary>
        /// 当前页
        /// </summary>
        public int CurrentPageIndex
        {
            get { return this.m_CurrentPageIndex; }
            set { this.m_CurrentPageIndex = value; }
        }


        /// <summary>
        /// 总条数
        /// </summary>
        public int PageCount
        {
            get { return this.m_PageCount; }
            set { this.m_PageCount = value; }
        }


        /// <summary>
        /// 分页查询条件
        /// </summary>
        public string Keywords
        {
            get { return this.m_Keywords; }
            set { this.m_Keywords = value; }
        }


        /// <summary>
        /// 每页显示条数
        /// </summary>
        public int PageSize
        {
            get { return this.m_PageSize; }
        }
        #endregion


        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Request.QueryString["id"] == null || !int.TryParse(Request.QueryString["id"], out m_InfoID))
                {
                    return;
                }

                InformationManageModule.UpdateHits(m_InfoID);
                this.m_Info = InformationManageModule.GetInfoByID(m_InfoID);
                InfoTitle = m_Info.Title;
                InfoContent = m_Info.InfoContent;
                CreateDate = m_Info.CreateTime.Value.ToString("yyyy-MM-dd");

                m_Title = TypeManageModule.GetTypeNameByID(m_Info.TypeID ?? 0);
                if (!IsPostBack)
                {
                    this.BindReapterForPageLoad();
                }
            }
            catch (Exception exception)
            {
                Session["WebErrorInfo"] = exception.Message.ToString();
                Response.Redirect("/Error.aspx");
            }
        }


        /// <summary>
        /// 新增评论
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                T_CF_Web_InfoCommnet info = new T_CF_Web_InfoCommnet();
                info.CommContent = txtContent.Text.Trim();

                info.InfoID = m_InfoID;
                info.CreatorID = Session["UserID"].ToInt();

                InformationManageModule.InsertNewInfoComm(info);
                this.BindReapterForPageLoad();

                txtContent.Text = "";
            }
            catch (Exception exception)
            {
                Session["WebErrorInfo"] = exception.Message.ToString();
                Response.Redirect("/Error.aspx");
            }
        }
        #endregion


        #region 方法
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindReapterForPageLoad()
        {
            if (Request.QueryString["page"] != null)
            {
                this.m_CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
            }
            else
            {
                this.m_CurrentPageIndex = 0;
            }

            // 查询数据库
            DataTable comTable = InformationManageModule.GetInfoComList(m_InfoID);

            // 绑定
            this.BindReapter(comTable);
        }


        /// <summary>
        /// BindReapter
        /// </summary>
        /// <param name="dt"></param>
        private void BindReapter(DataTable dt)
        {
            //分页
            PagedDataSource pagedDataSource = new PagedDataSource();
            pagedDataSource.DataSource = dt.DefaultView;
            pagedDataSource.AllowPaging = true;
            pagedDataSource.PageSize = this.m_PageSize;
            pagedDataSource.CurrentPageIndex = this.m_CurrentPageIndex;
            this.m_PageCount = dt.Rows.Count;

            //绑定数据

            rptList.DataSource = pagedDataSource;
            rptList.DataBind();
        }

        #endregion

    }
}